﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using xzz.webapi.Data;
using xzz.webapi.Models.Domain;
using xzz.webapi.Models.DTO;
using xzz.webapi.Repository;

namespace xzz.webapi.Controllers
{
    //http://localhost:prornumner/api/walks
    [Route("api/[controller]")]
    [ApiController]
    public class WalksController : ControllerBase
    {
        private readonly IWalkRepository walkRepository;
        private readonly IMapper mapper;

        public WalksController(IWalkRepository walkRepository, IMapper mapper)
        {
            this.walkRepository = walkRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 获取walk列表
        /// </summary>
        /// <param name="filterOn"></param>
        /// <param name="filterQuery"></param>
        /// <param name="sortBy"></param>
        /// <param name="isAscending"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetAll([FromQuery] string? filterOn, [FromQuery] string? filterQuery, [FromQuery] string? sortBy, [FromQuery] bool? isAscending, [FromQuery] int page = 1, [FromQuery] int pagesize = 10)
        {
            var walksDomain = await walkRepository.GetAllAsync(filterOn, filterQuery, sortBy, isAscending ?? true, page, pagesize);
            var walksDto = mapper.Map<List<WalkDto>>(walksDomain);
            return Ok(walksDto);
        }

        [HttpGet]
        [Route("{id:Guid}")]
        public async Task<IActionResult> GetById([FromRoute] Guid id)
        {
            var walkDomain = await walkRepository.GetByIdAsync(id);
            if (walkDomain == null)
            {
                return NotFound();
            }
            var walkDto = mapper.Map<WalkDto>(walkDomain);
            return Ok(walkDto);
        }

        [HttpPost]
        public async Task<IActionResult> Create([FromBody] AddWalksRequestDto addWalksRequestDto)
        {
            var walksDomainModel = mapper.Map<Walk>(addWalksRequestDto);
            walksDomainModel = await walkRepository.CreateAsync(walksDomainModel);
            var walksDto = mapper.Map<WalkDto>(walksDomainModel);
            return CreatedAtAction(nameof(GetById), new { id = walksDto.Id }, walksDto);
        }

        [HttpPut]
        [Route("{id:Guid}")]
        public async Task<IActionResult> Update([FromRoute] Guid id, [FromBody] UpdateWalksRequestDto updateWalksRequestDto)
        {
            var walksDomainModel = mapper.Map<Walk>(updateWalksRequestDto);
            walksDomainModel = await walkRepository.UpdateAsync(id, walksDomainModel);
            if (walksDomainModel == null)
            {
                return NotFound();
            }
            var walksDto = mapper.Map<WalkDto>(walksDomainModel);
            return Ok(walksDto);
        }

        [HttpDelete]
        [Route("{id:Guid}")]
        public async Task<IActionResult> Delete([FromRoute] Guid id)
        {
            var walksDomainModel = await walkRepository.DeleteAsync(id);
            if (walksDomainModel == null)
            {
                return NotFound();
            }
            var walksDto = mapper.Map<WalkDto>(walksDomainModel);
            return Ok(walksDto);
        }
    }
}
